﻿using System;
using System.Web.Mvc;

namespace Devsoft.Web.Mvc.DB
{
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)]
    class AutoCommitAttribute : ActionFilterAttribute, IActionFilter
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            Devsoft.DB.Models.Transaction.Current.Enter();
            base.OnActionExecuting(filterContext);
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            if (filterContext.Exception != null)
            {
                Devsoft.DB.Models.Transaction.Current.Rollback();
            }
            else
            {
                Devsoft.DB.Models.Transaction.Current.Commit();
            }
        }
    }

}
